const mongoose = require('mongoose')
const boom = require('boom')

const Note = require('../models/note')

const environment = process.env.NODE_ENV
const stage = require('../config')[environment]

module.exports = {
  create: (req, res, next) => {
    mongoose.connect(process.env.MONGOLAB_URI, stage.mongo_options).then(
      () => {
        let result = {}
        let status = 201
        const {
          user,
          book,
          noteText
        } = req.body
        const note = new Note({
          user,
          book,
          noteText
        })
        note.save((err, note) => {
          if (err) {
            mongoose.connection.close()
            return next(boom.badImplementation(err))
          }
          result.status = status
          result.data = note
          mongoose.connection.close()
          return res.status(status).send(result)
        })
      },
      err => {
        mongoose.connection.close()
        return next(boom.badImplementation(err))
      }
    )
  },
  delete: (req, res, next) => {
    mongoose.connect(process.env.MONGOLAB_URI, stage.mongo_options).then(
      () => {
        let result = {}
        let status = 200
        const {
          id
        } = req.params
        Note.findByIdAndDelete(id).
        exec(function (err, note) {
          if (err) {
            mongoose.connection.close()
            return next(boom.badImplementation(err))
          }
          result.status = status
          result.data = note
          mongoose.connection.close()
          return res.status(status).send(result)
        })
      },
      err => {
        mongoose.connection.close()
        return next(boom.badImplementation(err))
      }
    )
  },
  update: (req, res, next) => {
    mongoose.connect(process.env.MONGOLAB_URI, stage.mongo_options).then(
      () => {
        let result = {}
        let status = 200
        const {
          id
        } = req.params
        Note.findByIdAndUpdate(id, req.body).
        exec(function (err, note) {
          if (err) {
            mongoose.connection.close()
            return next(boom.badImplementation(err))
          }
          result.status = status
          result.data = note
          mongoose.connection.close()
          return res.status(status).send(result)
        })
      },
      err => {
        mongoose.connection.close()
        return next(boom.badImplementation(err))
      }
    )
  },
  show: (req, res, next) => {
    mongoose.connect(process.env.MONGOLAB_URI, stage.mongo_options).then(
      () => {
        let result = {}
        let status = 200
        const {
          id
        } = req.params
        Note.findById(id).
        exec(function (err, note) {
          if (err) {
            mongoose.connection.close()
            return next(boom.badImplementation(err))
          }
          result.status = status
          result.data = note
          mongoose.connection.close()
          return res.status(status).send(result)
        })
      },
      err => {
        mongoose.connection.close()
        return next(boom.badImplementation(err))
      }
    )
  },
  index: (req, res, next) => {
    mongoose.connect(process.env.MONGOLAB_URI, stage.mongo_options).then(
      () => {
        let result = {}
        let status = 200
        let {
          user = ''
        } = req.query
        const condition = {
          user
        }
        Note.find(condition).
        exec(function (err, notes) {
          if (err) {
            mongoose.connection.close()
            return next(boom.badImplementation(err))
          }
          result.status = status
          result.data = notes
          mongoose.connection.close()
          return res.status(status).send(result)
        })
      },
      err => {
        mongoose.connection.close()
        return next(boom.badImplementation(err))
      }
    )
  }
}
